const { promisify } = require("util");
const figlet = promisify(require("figlet"));
const clear = require("clear");
const chalk = require("chalk");
const log = content => console.log(chalk.green(content));
const {clone} = require("./download");


const spawn = async (...args) => {
    const {spawn} = require("child_process");
    return new Promise(resolve => {
        const proc = spawn(...args);
        proc.stdout.pipe(process.stdout);
        proc.stderr.pipe(process.stderr);
        proc.on("close", () => {
            resolve();
        })
    })
}

module.exports = async name => {
    // 打印欢迎界面
    clear();
    const data = await figlet("QXCLI Welcome");
    log(data);
    log(`正在创建项目：${name}`);

    // 初始化
    // await clone('github:big-silly/react-demo', name);
    await clone('direct:https://gitee.com/git-len/react-template.git', name);

    // 安装依赖
    log("安装依赖...")
    await spawn('yarn', ['install'], {cwd: `./${name}`});
    log(chalk.green(`
        安装完成：
        To get Start
        ============================
            cd ${name}
            yarn start
        ============================
    `))

    // 打开浏览器
    const open = require("open");
    open("http://localhost:3000");

    // 启动项目
    await spawn('yarn', ['start'], {cwd: `./${name}`});

}